package com.yiwei.zxp.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.adapter.BussinessMoneyRecordAdapter;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.MoneyCenterBean;
import com.yiwei.zxp.api.bean.ShareGoldExplainBean;
import com.yiwei.zxp.base.BaseActivity;
import com.yiwei.zxp.constant.Const;
import com.yiwei.zxp.constant.UserInfoGlobal;
import com.yiwei.zxp.util.Dialog;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.Util;
import com.yiwei.zxp.view.OnLoadeMoreListener;
import com.yiwei.zxp.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class BussinessMoneyRecordActivity extends BaseActivity {


    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    ImageView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.txt_balance)
    TextView txtBalance;
    @BindView(R.id.btn_withdraw)
    TextView btnWithdraw;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    private Unbinder unbiner;
    private BussinessMoneyRecordAdapter adapter;
    private OnLoadeMoreListener listener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            getMoneyCenter(page);
        }
    };
    private String money;

    //进账中心
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bussiness_money_record);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        adapter = new BussinessMoneyRecordAdapter(this);
        list.setAdapter(adapter);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                listener.setPage(1);
                getMoneyCenter(1);
            }
        });
        getMoneyCenter(1);
    }

    public void getMoneyCenter(int page) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        MoneyCenterBean bean = new MoneyCenterBean();
        bean.page = page;
        Api.api_service.getMoneyCenter(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    money = result.datas.money;
                    txtBalance.setText(money);
                    adapter.add(result.datas.lists, page == 1);
                    if (adapter.getItemCount() == 0) {
                        layoutEmpty.setVisibility(View.VISIBLE);
                    } else {
                        layoutEmpty.setVisibility(View.GONE);
                    }

                });
    }


    public void getExplain(String keyword) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        ShareGoldExplainBean bean = new ShareGoldExplainBean();
        bean.keyword = keyword;
        Api.api_service.getShareGoldExplain(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    Dialog.showPacketTips(this, result.datas.title, result.datas.content);
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.txt_right, R.id.btn_back, R.id.btn_withdraw})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_right:
                getExplain(Const.KeyWord.MONEY);
                break;
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_withdraw:
                if (TextUtils.isEmpty(money)) {
                    return;
                }
                if (Double.valueOf(money) < 100) {
                    Dialog.showDefaultDialog(this, "", "余额必须满100元才能提现喔！", "", "我知道了", false, new Dialog.DialogDefaultClickListener() {
                        @Override
                        public void confirm() {

                        }

                        @Override
                        public void cancel() {

                        }
                    });
                    return;
                }
                Util.startActivity(this, WithdrawBalanceActivity.class);
                break;
        }
    }
}
